<?php

namespace addons\cms\controller\wxapp;

use addons\third\library\Service;
use addons\third\model\Third;
use app\common\library\Auth;
use fast\Http;
use function print_r;
use think\Config;
use think\Validate;
use app\common\model\Area as AreaModel;

/**
 * 会员
 */
class Area extends Base
{
    protected $noNeedLogin = '*';


    /**
     * 获取地址
     */
    public function index()
    {
        $list = collection(AreaModel::with('children')->field(['city_id', 'name', 'id', 'parent_id'])->where('parent_id', 0)->order('id asc')->select())->toArray();
        $data = [];
        foreach ($list as &$item) {
            $value = ['v' => $item['city_id'], 'n' => $item['name']];
            if ($item['children']) {
                foreach ($item['children'] as $key => &$child) {
                    $value['c'][$key] = ['v' => $child['city_id'], 'n' => $child['name']];
                    unset($child['id'], $child['area_code'], $child['merger_name'], $child['is_show'], $child['level'], $child['lng'], $child['lat'], $child['lat']);
                    if (AreaModel::where('parent_id', $child['city_id'])->count()) {
                        $child['children'] = collection(AreaModel::where('parent_id', $child['city_id'])->field(['city_id', 'name', 'id', 'parent_id'])->select())->toArray();
                        foreach ($child['children'] as $kk => $vv) {
                            $value['c'][$key]['c'][$kk] = ['v' => $vv['city_id'], 'n' => $vv['name']];
                        }
                    }
                }
            }
            $data[] = $value;
        }

        $this->success('省市区', $data);
    }

}
